package com.example.demo.controller.emptCon;

import com.example.demo.annotate.Log;
import com.example.demo.pojo.LogiEmpt;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.emp.Empt;
import com.example.demo.pojo.emp.PageBeen;
import com.example.demo.service.EmpService;
import com.example.demo.service.impl.EmpServiceImpl;
import com.example.demo.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/empt")
public class EmptController {
    @Autowired
    private EmpService empService;

    @GetMapping("/login")
    public Result login(String username) {
        Empt login = empService.login(username);


        System.out.println(login);
        if (login != null) {
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",login.getId());
            claims.put("username",login.getUsername());
            claims.put("name",login.getName());
            String token = JwtUtils.generateJwt(claims);
            LogiEmpt logiEmpt = new LogiEmpt(login, token);
            return Result.success(logiEmpt);
        }
        return Result.error("登录失败",null);
    }


    @GetMapping("/add")
    @Log
    public Result addEmpt(Empt user) {
        Boolean success = empService.addEmpt(user);
        if(success) return Result.success();
        else return Result.error("添加失败",null);
    }

    @GetMapping("/selectList")
    public Result selectList(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size
                             ,String username,String name,String gender,String job,Integer depId
    ) {
        PageBeen pageList = empService.listPage(page,size,username,name,gender,job,depId);
        return Result.success(pageList);
    }

}
